package com.example.dllo.my36kr.find;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.dllo.my36kr.R;
import com.example.dllo.my36kr.share.all.AllWebActivity;
import com.example.dllo.my36kr.find.activities.NearTermActivity;
import com.example.dllo.my36kr.find.findshare.FindShareAty;
import com.example.dllo.my36kr.main.MainActivity;
import com.example.dllo.my36kr.news.NewsPicBean;
import com.example.dllo.my36kr.news.NewsViewPager;
import com.example.dllo.my36kr.share.all.AllBean;
import com.example.dllo.my36kr.tools.BaseFragment;
import com.example.dllo.my36kr.tools.MyApp;
import com.example.dllo.my36kr.tools.NetTool;
import com.example.dllo.my36kr.tools.StaticUrl;

import java.util.ArrayList;

/**
 * Created by dllo on 16/8/15.
 */

/**
 * 发现
 */
public class FindFragment extends BaseFragment implements View.OnClickListener {


    private ArrayList<String> arrayList;
    private ViewPager viewPager;
    private NewsViewPager pager;
    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
            Message  message = handler.obtainMessage();
            message.what = 1;
            handler.sendMessageDelayed(message,5000);
            return false;
        }
    });
    private LinearLayout activitiesLl;
    private ImageView logoOne;
    private TextView nameOne;
    private TextView briefOne;
    private TextView adnamefOne;
    private TextView adcontentfOne;
    private ImageView logoTwo;
    private TextView nameTwo;
    private TextView briefTwo;
    private TextView adnamefTwo;
    private TextView adcontentfTwo;
    private ImageView logoThree;
    private TextView nameThree;
    private TextView briefThree;
    private TextView adnamefThree;
    private TextView adcontentfThree;
    private AllBean data;


    @Override
    protected int setLayout() {
        return R.layout.fragment_find;
    }

    @Override
    protected void initView() {
        viewPager = bindView(R.id.vp_find);//轮播图
        pager = new NewsViewPager(getContext());
        //近期活动
        activitiesLl = bindView(R.id.ll_find_activities_near);//近期活动按钮
        LinearLayout shareLl = bindView(R.id.ll_find_activities_share);//36氪研究所
        LinearLayout find = bindView(R.id.ll_find_fragment_find);//寻找投资人
        find.setOnClickListener(this);
        shareLl.setOnClickListener(this);
        activitiesLl.setOnClickListener(this);
        LinearLayout all = bindView(R.id.ll_find_fragment_all);//查看全部
        all.setOnClickListener(this);
        LinearLayout one = bindView(R.id.ll_find_fragment_one);//热门项目下面的三个
        one.setOnClickListener(this);
        LinearLayout two = bindView(R.id.ll_find_fragment_two);
        two.setOnClickListener(this);
        LinearLayout three = bindView(R.id.ll_find_fragment_three);
        three.setOnClickListener(this);


        logoOne = bindView(R.id.iv_find_company_one);
        nameOne = bindView(R.id.tv_find_company_name_one);
        briefOne = bindView(R.id.tv_find_company_brief_one);
        adnamefOne = bindView(R.id.tv_find_adname_one);
        adcontentfOne = bindView(R.id.tv_find_adcontent_one);
        logoTwo = bindView(R.id.iv_find_company_two);
        nameTwo = bindView(R.id.tv_find_company_name_two);
        briefTwo = bindView(R.id.tv_find_company_brief_two);
        adnamefTwo = bindView(R.id.tv_find_adname_two);
        adcontentfTwo = bindView(R.id.tv_find_adcontent_two);
        logoThree = bindView(R.id.iv_find_company_three);
        nameThree = bindView(R.id.tv_find_company_name_three);
        briefThree = bindView(R.id.tv_find_company_brief_three);
        adnamefThree = bindView(R.id.tv_find_adname_three);
        adcontentfThree = bindView(R.id.tv_find_adcontent_three);




    }
    @Override
    public void onStart() {
        super.onStart();
        Message message = handler.obtainMessage();//线程 轮播图
        message.what = 1;
        handler.sendMessageDelayed(message , 5000);
    }

    @Override
    public void onStop() {
        super.onStop();
        handler.removeMessages(1);//停止线程
    }

    @Override
    protected void initData() {
        arrayList = new ArrayList<>();
        //轮播解析
        mNetTool.getNetData(StaticUrl.NEWS_LUNBOTU_URL, NewsPicBean.class, new NetTool.NetListener<NewsPicBean>() {
            @Override
            public void onSuccess(NewsPicBean newsPicBean) {
                //图片加在一个集合
                for (int i = 0; i < newsPicBean.getData().getPics().size(); i++) {
                    arrayList.add(newsPicBean.getData().getPics().get(i).getImgUrl());
                }
                pager.setmList(arrayList);
                pager.setViewPager(viewPager);
                viewPager.setAdapter(pager);
            }

            @Override
            public void onError(String errorMsg) {

            }
        });
        //热门下面的三个解析
        mNetTool.getNetData(StaticUrl.SHARE_ALL_FIND_URL, AllBean.class, new NetTool.NetListener<AllBean>() {
            @Override
            public void onSuccess(AllBean allBean) {
                data = allBean;
                mNetTool.getImage(allBean.getData().getData().get(0).getCompany_logo(),logoTwo);
                nameOne.setText(allBean.getData().getData().get(0).getCompany_name());
                briefOne.setText(allBean.getData().getData().get(0).getCompany_brief());
                adnamefOne.setText(allBean.getData().getData().get(0).getCf_advantage().get(0).getAdname());
                adcontentfOne.setText(allBean.getData().getData().get(0).getCf_advantage().get(0).getAdcontent());

                mNetTool.getImage(allBean.getData().getData().get(1).getCompany_logo(),logoOne);
                nameTwo.setText(allBean.getData().getData().get(1).getCompany_name());
                briefTwo.setText(allBean.getData().getData().get(1).getCompany_brief());
                adnamefTwo.setText(allBean.getData().getData().get(1).getCf_advantage().get(0).getAdname());
                adcontentfTwo.setText(allBean.getData().getData().get(1).getCf_advantage().get(0).getAdcontent());

                mNetTool.getImage(allBean.getData().getData().get(2).getCompany_logo(),logoThree);
                nameThree.setText(allBean.getData().getData().get(2).getCompany_name());
                briefThree.setText(allBean.getData().getData().get(2).getCompany_brief());
                adnamefThree.setText(allBean.getData().getData().get(2).getCf_advantage().get(0).getAdname());
                adcontentfThree.setText(allBean.getData().getData().get(2).getCf_advantage().get(0).getAdcontent());

            }
            @Override
            public void onError(String errorMsg) {

            }
        });

        
    }

    @Override
    public void onClick(View v) {//点击事件
        switch (v.getId()) {
            case R.id.ll_find_activities_near:
                startActivity(new Intent(getActivity(), NearTermActivity.class));
                break;
            case R.id.ll_find_activities_share:
                Intent intent = new Intent(getActivity(), FindShareAty.class);
                intent.putExtra("find",StaticUrl.NEWS_RESEARCH_URL);
                startActivity(intent);
                break;
            case R.id.ll_find_fragment_find:
                Intent intent1 = new Intent(getActivity(), FindShareAty.class);
                intent1.putExtra("find",StaticUrl.FIND_PEOPLE_URL);
                startActivity(intent1);
                break;
            case R.id.ll_find_fragment_all:
                MainActivity mainActivity = (MainActivity) getActivity();
                mainActivity.replaceOne();
                break;
            case R.id.ll_find_fragment_one:
                Intent intent4 = new Intent(getActivity(), AllWebActivity.class);
                intent4.putExtra("id",String.valueOf(data.getData().getData().get(0).getCrowdFundingId()));
                startActivity(intent4);
                break;
            case R.id.ll_find_fragment_two:
                Intent intent2 = new Intent(getActivity(), AllWebActivity.class);
                intent2.putExtra("id",String.valueOf(data.getData().getData().get(1).getCrowdFundingId()));
                startActivity(intent2);
                break;
            case R.id.ll_find_fragment_three:
                Intent intent3 = new Intent(getActivity(), AllWebActivity.class);
                intent3.putExtra("id",String.valueOf(data.getData().getData().get(2).getCrowdFundingId()));
                startActivity(intent3);
                break;
        }


    }
}
